<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
 
/**
 * Category Controller
 */
class ShopControllerAddress extends JControllerForm
{
	/**
	*	function save address
	*	by anhtran
	***/
	public function save(){
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
	// get params
		$id =  JRequest::getVar('id');
		$title = trim(JRequest::getVar('title',''));
		$old_parent = JRequest::getVar('old_parent','');
		$parent_id = JRequest::getVar('parent_id',0);
		//------------------
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
		$row = JTable::getInstance('Address','ShopTable');
		$row->reorder();
		$row->bind(JRequest::get('post'));	
		// insert node
		if($id == ''){
			//begin add
			//check exists address in DB
			$query->clear();
			$query->select('*');
			$query->from('#__shop_address');
			$query->where("title = '$title'");
			$db->setQuery((string)$query);
			$pro = $db->loadObject();
			if(count($pro)){
				JError::raiseWarning(404, JText::_('COM_SHOP_ADDRESS_EXISTS_CONFLICT'));
				$this->setRedirect('index.php?option=com_shop&view=addresses');
			}
			
			// insert  parent == 0
			if($parent_id == 0)
			{
				// insert address in database with parent_id = 0;
				$query = "select * from #__shop_address order by rgt desc limit 1";
				$db->setQuery($query);
				$aro = $db->loadObject();
				if(!count($aro))
					$right = 0;
				else
					$right = $aro->rgt;
					
				$row->lft = $right +1;
				$row->rgt = $right + 2;
				$row->parent_id = 0;
				$row->level = 1;
				$row->ordering = 1;
				if (!$row->store()) {
					echo  "<script>alert({$row->getError()});window.history.go(-1)</script>\n";
				}
				$this->setMessage(JText::_('COM_SHOP_ADDRESS_SAVE_SUCCESS'));
				$this->setRedirect('index.php?option=com_shop&view=addresses');
			}
			else{
				// insert parent != 0
				//echo 'insert node width parent = '.$parent_id;
				$query->clear();
				$query->select('*');
				$query->from('#__shop_address');
				$query->where("id = $parent_id");
				$db->setQuery((string)$query);
				$pr = $db->loadObject();
				if(count($pr) === 0){
					JError::raiseWarning(500, JText::_('COM_SHOP_ADDRESS_NOT_FOUND_PARENT'));
					$this->setRedirect('index.php?option=com_shop&view=addresses');
				}else{
					//update nod of parent 
					$left = $pr->lft;
					
					$query->clear();
					$query->update('#__shop_address');
					$query->set('rgt = rgt + 2');
					$query->where('rgt > '.$left);
					$db->setQuery((string)$query);
					$db->query();
					
					$query->clear();
					$query->update('#__shop_address');
					$query->set('lft = lft + 2');
					$query->where('lft > '.$left);
					$db->setQuery((string)$query);
					$db->query();
					
					$row->lft = $left + 1;
					$row->rgt = $left + 2;
					$row->level = $pr->level + 1;
					$row->parent_id = $pr->id;
					$row->ordering = 1;
					if (!$row->store()) {
							echo  "<script>alert({$row->getError()});window.history.go(-1)</script>\n";
						}
						$this->setMessage(JText::_('COM_SHOP_ADDRESS_SAVE_SUCCESS'));
						$this->setRedirect('index.php?option=com_shop&view=addresses');
				}
			}
			//end add
		}
		else{
			//update address or node
			//check parent new and old parent
			if($parent_id == $old_parent)
			{
				$query->clear();
				$query->select('*');
				$query->from('#__shop_address');
				$query->where('id = '.$id);
				$db->setQuery((string)$query);
				$cur = $db->loadObject();
				$row->lft = $cur->lft;
				$row->rgt = $cur->rgt;
				$row->level = $cur->level;
				$row->ordering = $cur->ordering;
				//no change parent
				if (!$row->store()) {
					echo  "<script>alert({$row->getError()});window.history.go(-1)</script>\n";
				}
				$this->setMessage(JText::_('COM_SHOP_ADDRESS_SAVE_SUCCESS'));
				$this->setRedirect('index.php?option=com_shop&view=addresses');
				
			}else{
				//parent new != old parent
				//echo 'change parent';
				JError::raiseWarning(500, JText::_('COM_SHOP_ADDRESS_NOT_CHANGE_PARENT'));
				$this->setRedirect('index.php?option=com_shop&view=addresses');
			}
		
		}
		
		
	}

}
